import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Status from '../views/Status.vue'
import QueueManagement from '../views/QueueManagement.vue'
import Debug from '../views/Debug.vue'
import AdminLogin from '../views/AdminLogin.vue'
import AdminPanel from '../views/AdminPanel.vue'
import { checkAdminAuth } from '../utils/auth'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/status',
    name: 'Status',
    component: Status
  },
  {
    path: '/queue',
    name: 'QueueManagement',
    component: QueueManagement,
    meta: { requiresAdmin: true }
  },
  {
    path: '/debug',
    name: 'Debug',
    component: Debug,
    meta: { requiresAdmin: true }
  },
  {
    path: '/admin0230',
    name: 'AdminLogin',
    component: AdminLogin
  },
  {
    path: '/admin',
    name: 'AdminPanel',
    component: AdminPanel,
    meta: { requiresAdmin: true }
  }
]

const router = createRouter({
  history: createWebHistory('/'),  // 设置基础路径为根路径
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin) {
    if (checkAdminAuth()) {
      next()
    } else {
      // 未认证，重定向到管理员登录页面
      next({ name: 'AdminLogin' })
    }
  } else {
    next()
  }
})

export default router


